package server.process;

import java.io.IOException;
import java.io.InputStream;

/**
 * 把请求信息封装为Request对象（根据InputSteam输入流封装）
 */
public class RequestMapper extends Request{

    //访问的地址
    private String host;

    //访问那个项目
    private String project;


    public RequestMapper(String project) {
        this.project = project;
    }

    public RequestMapper() {
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getProject() {
        return project;
    }

    public void setProject(String project) {
        this.project = project;
    }

    // 构造器，输入流传入
    public RequestMapper(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;
        // 从输入流中获取请求信息
        int count = 0;
        while (count == 0) {
            count = inputStream.available();
        }

        byte[] bytes = new byte[count];
        inputStream.read(bytes);

        String inputStr = new String(bytes);
        // 获取第一行请求头信息
        String firstLineStr = inputStr.split("\\n")[0];  // GET / HTTP/1.1
        String[] strings = firstLineStr.split(" ");
        this.method = strings[0];
        String url = strings[1];
        String[] split = url.trim().split("/");
        if(split.length > 2) {
            this.project = split[1];
            this.url = split[2];
        }else{
            this.url = split[1];
        }
        //处理第二行
        String secondLineStr = inputStr.split("\\n")[1];
        String[] seconsStrings = secondLineStr.split(" ");
        this.host = seconsStrings[1].trim();

        System.out.println("=====>>method:" + method);
        System.out.println("=====>>url:" + url);
        System.out.println("=====>>host:" + host);
        System.out.println("=====>>project:" + project);
    }

}
